/// ===========================================================================
/// THIS CODE AND INFORMATION ARE PROVIDED "AS IS" WITHOUT WARRANTY OF ANY
/// KIND, WHETHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
/// IMPLIED WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A PARTICULAR
/// PURPOSE.
/// ===========================================================================
/// 
/// Project:        SharePoint Faceted Search Config
/// Author:         Leonid Lyublinski (leonidly@microsoft.com)
/// Company:        Microsoft Services
/// Date:           10/24/2007
/// Version:        2.0
/// 
/// ===========================================================================
/// 
using System;
using System.IO;
using System.Text.RegularExpressions;
using System.Collections;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.ComponentModel;
using System.Text;
using System.Web;
using System.Xml;
using System.Xml.XPath;
using System.Xml.Xsl;
using System.Xml.Serialization;
using System.Data;
using System.Security;
using System.Security.Permissions;
using Microsoft.SharePoint.WebPartPages;
using Microsoft.SharePoint;
using Microsoft.SharePoint.WebControls;
using System.Globalization;
using Microsoft.SharePoint.Administration;
//using Microsoft.SharePoint.Security;
using Microsoft.SharePoint.Utilities;
using System.Reflection;

namespace Microsoft.SharePoint.Portal.ExtendedSearch
{

    /// <summary>
    /// A Feature Reciever for Faceted Search
    /// </summary>
    [PermissionSet(SecurityAction.Demand)]
    public sealed class FeatureReceiver : SPFeatureReceiver
    {
        public const string WEB_PART_GALLERY_NAME = "Web Part Gallery";
        public const string SEARCH_FACETS_WEBPART_NAME = "Search Facets";
        public const string SEARCH_BREADCRUMBS_WEBPART_NAME = "Search Bread Crumbs";

        public override void FeatureActivated(SPFeatureReceiverProperties properties)
        {
            //throw new Exception("The method or operation is not implemented.");
        }

        //[SharePointPermission(SecurityAction.Demand, ObjectModel = true)]
        public override void FeatureDeactivating(SPFeatureReceiverProperties properties)
        {
            RemoveWebParts(properties);
        }

        public override void FeatureInstalled(SPFeatureReceiverProperties properties)
        {
            //throw new Exception("The method or operation is not implemented.");
        }

        public override void FeatureUninstalling(SPFeatureReceiverProperties properties)
        {
            RemoveWebParts(properties);
        }

        private void RemoveWebParts(SPFeatureReceiverProperties properties)
        {
            SPSite site = properties.Feature.Parent as SPSite;
            if (site == null) return;
            try
            {
                // check if the list already exists
                using (SPWeb web = site.OpenWeb())
                {
                    SPList list = web.Lists[WEB_PART_GALLERY_NAME];
                    if (list == null) return;
                    SPListItemCollection items = list.Items;
                    for (int i = 0; i < items.Count; i++)
                    {
                        SPListItem item = items[i];
                        if (item.Title.ToLower() == SEARCH_FACETS_WEBPART_NAME.ToLower() ||
                            item.Title.ToLower() == SEARCH_BREADCRUMBS_WEBPART_NAME.ToLower())
                        {
                            item.Delete();
                            i--;
                        }
                    }
                    list.Update();
                }
            }
            catch (Exception ex)
            {
                Microsoft.Office.Server.Diagnostics.PortalLog.LogString("Faceted Search Receiver Exception: {0} ::: {1}", ex.Message, ex.StackTrace);
                throw ex;
            }
        }



    }
}